﻿using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using TQ.Sharepoint.Webparts.TwitterWebpart.Repositories;
using TQ.Sharepoint.Webparts.TwitterWebpart.Utilities;

namespace TQ.Sharepoint.Webparts.TwitterWebpart
{
    public class CustomToolpart : ToolPart
    {
        private DropDownList ddlTrends;
        private TextBox tbQuery;
        private TextBox tbMaxTweets;
        private TextBox tbTweetsPerPage;


        protected override void CreateChildControls()
        {
            var wp = (TwitterWebpart)ParentToolPane.SelectedWebPart;
            tbQuery = new TextBox();
            tbMaxTweets = new TextBox();
            tbTweetsPerPage = new TextBox();
            ddlTrends = new DropDownList();
            ddlTrends.Items.Add(new ListItem(string.Empty,string.Empty));
            ddlTrends.Items.AddRange(new TrendsRepositories().GetTrending());
            ddlTrends.DataBind();

            var lcDdl = new LiteralControl("Trending:".EnclosedDiv());
            Controls.Add(lcDdl);
            if (ddlTrends.Items.Count == 2)
            {
                var errorMsg = new LiteralControl(ddlTrends.Items[1].Text.EnclosedDiv());
                Controls.Add(errorMsg);
            }
            else
            {
                Controls.Add(ddlTrends);
            }

        
            tbQuery.Text = wp.Query;
            var keyWords = new LiteralControl("Keywords:".EnclosedDiv());
            Controls.Add(keyWords);
            Controls.Add(tbQuery);

            tbMaxTweets.Text = wp.MaxTweets;
            var maxTweets = new LiteralControl("Max Tweets:".EnclosedDiv());
            Controls.Add(maxTweets);
            Controls.Add(tbMaxTweets);

            tbTweetsPerPage.Text = wp.TweetsPerPage;
            var tweetsPerPage = new LiteralControl("Tweets Per Page:".EnclosedDiv());
            Controls.Add(tweetsPerPage);
            Controls.Add(tbTweetsPerPage);
            if (ddlTrends.Items.Count > 2)
            {
                var globalScript = new StringBuilder();
                globalScript.Append("<script type='text/javascript'>");
                globalScript.Append("$('#" + ddlTrends.ClientID + "')" +
                                    ".change(function(){$('#" + tbQuery.ClientID + "')" +
                                    ".val(unescape(decodeURI($('#" + ddlTrends.ClientID + "').val())))});");
                globalScript.Append("</script>");
                var litTqweetGlobal = new LiteralControl {Text = globalScript.ToString()};
                Controls.Add(litTqweetGlobal);
            }
            base.CreateChildControls();

        }

        public override void ApplyChanges()
        {
            var wp = (TwitterWebpart) ParentToolPane.SelectedWebPart;
            int isNum;
            wp.Query = tbQuery.Text;
            wp.MaxTweets = (int.TryParse(tbMaxTweets.Text, out isNum)) ? tbMaxTweets.Text : Str.DefaultMaxTweets;
            wp.TweetsPerPage = (int.TryParse(tbTweetsPerPage.Text, out isNum)) ? tbTweetsPerPage.Text : Str.DefaultMaxTweets;
        }


    }
}

